package com.mall.shopping.services;

import com.mall.shopping.IHomeService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.converter.ContentConverter;
import com.mall.shopping.converter.ProductConverter;
import com.mall.shopping.dal.entitys.Item;
import com.mall.shopping.dal.entitys.Panel;
import com.mall.shopping.dal.entitys.PanelContentItem;
import com.mall.shopping.dal.persistence.ItemMapper;
import com.mall.shopping.dal.persistence.PanelContentMapper;
import com.mall.shopping.dal.persistence.PanelMapper;
import com.mall.shopping.dto.HomePageResponse;
import com.mall.shopping.dto.PanelDto;
import com.mall.shopping.dto.ProductDto;
import org.apache.commons.lang3.builder.ToStringExclude;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class IHomePageServiceImpl implements IHomeService {

    @Autowired
    PanelMapper panelMapper;
    @Autowired
    PanelContentMapper panelContentMapper;
    @Autowired
    ItemMapper itemMapper;
    @Autowired
    ContentConverter contentConverter;
    @Autowired
    ProductConverter productConverter;

    @Override
    public HomePageResponse homepage() {
        //new 响应对象，将所有执行结果状态塞进去。
        HomePageResponse homePageResponse = new HomePageResponse();

        try {
            List<Panel> panels = panelMapper.selectAll();
            Set<PanelDto> panelDtoSet = new HashSet<>();
            //PanelContentItem中有三个参数需要通过该表提供的productId查询拿到
            //productName,salePrice,subTitle,这三个与查询的item表中字段对不上，需要映射。
            for (Panel panel : panels) {
                List<PanelContentItem> panelContentItems = panelContentMapper.selectPanelContentAndProductWithPanelId(panel.getId());
                for (PanelContentItem panelContentItem : panelContentItems) {
                    Long productId = panelContentItem.getProductId();
                    //由于tb_panel_content数据表中,product存在null,所以需要判断。
                    if(productId == null) continue;
                    Item item = itemMapper.selectByPrimaryKey(productId);
                    ProductDto productDto = productConverter.item2Dto(item);
                    panelContentItem.setSubTitle(productDto.getSubTitle());
                    panelContentItem.setSalePrice(productDto.getSalePrice());
                    panelContentItem.setProductName(productDto.getProductName());
                }
                panel.setPanelContentItems(panelContentItems);
                PanelDto panelDto = contentConverter.panen2Dto(panel);
                panelDtoSet.add(panelDto);
            }
            homePageResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
            homePageResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
            homePageResponse.setPanelContentItemDtos(panelDtoSet);
        } catch (Exception e) {
            e.printStackTrace();
            homePageResponse.setCode(ShoppingRetCode.DB_EXCEPTION.getCode());
            homePageResponse.setMsg(ShoppingRetCode.DB_EXCEPTION.getMessage());
        }
        return homePageResponse;
    }


}
